package v1

import (
	"context"
	"encoding/json"
	"fmt"
	"github.com/gin-gonic/gin"
	"github.com/jinzhu/copier"
	"maya-service/bll"
	"maya-service/model"
	"maya-service/model/po"
	"maya-service/server/web/middleware"
	"maya-service/utils"
	"maya-service/utils/request"
)

var AnswerRecord = &answerRecord{}

func init() {
	RegisterRouter(AnswerRecord)
}

type answerRecord struct{}

// Init
func (a *answerRecord) Init(r *gin.RouterGroup) {
	g := r.Group("/answerRecord", middleware.Auth())
	{
		g.POST("/create", a.create)
		g.POST("/update", a.update)
		g.POST("/list", a.list)
		g.POST("/delete", a.delete)
		g.POST("/detail", a.find)
	}
}

// create
func (a *answerRecord) create(c *gin.Context) {
	var (
		in  = &po.AnswerRecordCreateRequest{}
		err error
	)

	if err = c.ShouldBindJSON(in); err != nil {
		c.Error(err)
		return
	}

	// 获取用户ID
	userId, err := request.GetUserId(c.Request.Context())
	if err != nil {
		c.Error(err)
		return
	}
	in.UserId = userId

	answerData, _ := json.Marshal(in.AnswerValues)
	in.AnswerValue = string(answerData)
	req := &model.AnswerRecordCreateRequest{}
	copier.Copy(req, in)

	// 先判断记录是否存在，存在就更新否则创建
	find, _ := bll.AnswerRecord.Find(c.Request.Context(), &model.AnswerRecordInfoRequest{
		UserId:      in.UserId,
		FirstLevel:  in.FirstLevel,
		SecondLevel: in.SecondLevel,
	})

	if find != nil && find.Id > 0 {
		updateData := &model.AnswerRecordUpdateRequest{
			Id:          find.Id,
			UserId:      &in.UserId,
			FirstLevel:  &in.FirstLevel,
			SecondLevel: &in.SecondLevel,
			AnswerValue: &in.AnswerValue,
			CorrectNum:  &in.CorrectNum,
			Total:       &in.Total,
			Star:        &in.Star,
		}

		// 更新
		err = bll.AnswerRecord.Update(c.Request.Context(), updateData)
		if err != nil {
			c.Error(err)
			return
		}
		utils.ResponseOk(c, nil)
		return
	}

	// 创建
	if err = bll.AnswerRecord.Create(c.Request.Context(), req); err != nil {
		c.Error(err)
		return
	}

	go func() {
		defer utils.ExceptionCatch()
		bll.UserLogin(context.Background(), userId)
	}()
	utils.ResponseOk(c, nil)
}

// update
func (a *answerRecord) update(c *gin.Context) {
	var (
		in  = &po.AnswerRecordUpdateRequest{}
		err error
	)

	if err = c.ShouldBindJSON(in); err != nil {
		c.Error(err)
		return
	}

	if len(in.AnswerValues) > 0 {
		answerData, _ := json.Marshal(in.AnswerValues)
		strData := string(answerData)
		in.AnswerValue = &strData
	}

	req := &model.AnswerRecordUpdateRequest{}
	copier.Copy(req, in)

	if err = bll.AnswerRecord.Update(c.Request.Context(), req); err != nil {
		c.Error(err)
		return
	}
	utils.ResponseOk(c, nil)
}

// list
func (a *answerRecord) list(c *gin.Context) {
	var (
		in  = &model.AnswerRecordListRequest{}
		out = &model.AnswerRecordListResponse{}
		err error
	)

	if err = c.ShouldBindJSON(in); err != nil {
		c.Error(err)
		return
	}

	// 获取用户ID
	if c.GetHeader("source") != "web" {
		userId, err := request.GetUserId(c.Request.Context())
		if err != nil {
			c.Error(err)
			return
		}
		in.UserId = &userId
	}

	if out, err = bll.AnswerRecord.List(c.Request.Context(), in); err != nil {
		c.Error(err)
		return
	}
	utils.ResponseOk(c, out)
}

// list
func (a *answerRecord) find(c *gin.Context) {
	var (
		in  = &model.AnswerRecordInfoRequest{}
		out = &model.AnswerRecordInfo{}
		err error
	)

	if err = c.ShouldBindJSON(in); err != nil {
		c.Error(err)
		return
	}

	// 获取用户ID
	userId, err := request.GetUserId(c.Request.Context())
	if err != nil {
		c.Error(err)
		return
	}
	in.UserId = userId

	if out, err = bll.AnswerRecord.Find(c.Request.Context(), in); err != nil {
		c.Error(err)
		return
	}

	fmt.Println(json.Unmarshal([]byte(out.AnswerValue), &out.AnswerValues))
	utils.ResponseOk(c, out)
}

// delete
func (a *answerRecord) delete(c *gin.Context) {
	var (
		in  = &model.AnswerRecordDeleteRequest{}
		err error
	)

	if err = c.ShouldBindJSON(in); err != nil {
		c.Error(err)
		return
	}

	if err = bll.AnswerRecord.Delete(c.Request.Context(), in); err != nil {
		c.Error(err)
		return
	}
	utils.ResponseOk(c, nil)
}
